{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 财务数据与因子分析策略1-财务数据获取与排序策略"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from gm.api import *\n",
    "set_token(\"eea4b28a3ceb7048603d388bb777ebc67de47385\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取股票信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get_instruments - 查询最新交易标的信息\n",
    "\n",
    "get_instruments(symbols=None, exchanges=None, sec_types=None, names=None, skip_suspended=True, skip_st=True, fields=None, df=False)\n",
    "\n",
    "主要作用：\n",
    "\n",
    "* 获得某类股票的代码\n",
    "* 获取股票是否停牌，发行时间等基本信息\n",
    "\n",
    "exchange\n",
    "\n",
    "市场中文名\t市场代码\n",
    "\n",
    "上交所\tSHSE\n",
    "\n",
    "深交所\tSZSE\n",
    "\n",
    "中金所\tCFFEX\n",
    "\n",
    "上期所\tSHFE\n",
    "\n",
    "大商所\tDCE\n",
    "\n",
    "郑商所\tCZCE\n",
    "\n",
    "上海国际能源交易中心\tINE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function wrapper in module gm.api.query:\n",
      "\n",
      "wrapper(*args, **kw)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(get_instruments)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#get_history_constituents(index='SHSE.000001', start_date='2017-07-10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>adj_factor</th>\n",
       "      <th>multiplier</th>\n",
       "      <th>is_suspended</th>\n",
       "      <th>margin_ratio</th>\n",
       "      <th>position</th>\n",
       "      <th>lower_limit</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>settle_price</th>\n",
       "      <th>upper_limit</th>\n",
       "      <th>sec_level</th>\n",
       "      <th>...</th>\n",
       "      <th>board</th>\n",
       "      <th>sec_name</th>\n",
       "      <th>price_tick</th>\n",
       "      <th>delisted_date</th>\n",
       "      <th>call_or_put</th>\n",
       "      <th>sec_abbr</th>\n",
       "      <th>option_margin_ratio2</th>\n",
       "      <th>sec_type</th>\n",
       "      <th>conversion_start_date</th>\n",
       "      <th>is_adjusted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14.0926</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>7.26</td>\n",
       "      <td>8.07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.88</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>浦发银行</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>pfyx</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>None</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 32 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   adj_factor  multiplier  is_suspended  margin_ratio  position  lower_limit  \\\n",
       "0     14.0926         1.0             0           1.0         0         7.26   \n",
       "\n",
       "   pre_close  settle_price  upper_limit  sec_level  ... board sec_name  \\\n",
       "0       8.07           0.0         8.88          1  ...     1     浦发银行   \n",
       "\n",
       "  price_tick             delisted_date  call_or_put sec_abbr  \\\n",
       "0       0.01 2038-01-01 00:00:00+08:00                  pfyx   \n",
       "\n",
       "  option_margin_ratio2 sec_type  conversion_start_date  is_adjusted  \n",
       "0                  0.0        1                   None            0  \n",
       "\n",
       "[1 rows x 32 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取单个股票信息\n",
    "get_instruments(symbols='SHSE.600000', df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>adj_factor</th>\n",
       "      <th>multiplier</th>\n",
       "      <th>is_suspended</th>\n",
       "      <th>margin_ratio</th>\n",
       "      <th>position</th>\n",
       "      <th>lower_limit</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>settle_price</th>\n",
       "      <th>upper_limit</th>\n",
       "      <th>sec_level</th>\n",
       "      <th>...</th>\n",
       "      <th>board</th>\n",
       "      <th>sec_name</th>\n",
       "      <th>price_tick</th>\n",
       "      <th>delisted_date</th>\n",
       "      <th>call_or_put</th>\n",
       "      <th>sec_abbr</th>\n",
       "      <th>option_margin_ratio2</th>\n",
       "      <th>sec_type</th>\n",
       "      <th>conversion_start_date</th>\n",
       "      <th>is_adjusted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>121.3988</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>14.18</td>\n",
       "      <td>15.7500</td>\n",
       "      <td>0.0</td>\n",
       "      <td>17.33</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>平安银行</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>payx</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>151.4445</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>18.59</td>\n",
       "      <td>20.6600</td>\n",
       "      <td>0.0</td>\n",
       "      <td>22.73</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>万科A</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>wkA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7.4454</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>17.60</td>\n",
       "      <td>19.5600</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21.52</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>国华网安</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>ghwa</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>49.2924</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.88</td>\n",
       "      <td>5.4200</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.96</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>深振业A</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>szyA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26.8933</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.30</td>\n",
       "      <td>2.5500</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.81</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>神州高铁</td>\n",
       "      <td>0.01</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>szgt</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4020</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1564.6021</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>信息安全</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>xxaq</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4021</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4331.6421</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>基建工程</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>jjgc</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4022</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2693.2241</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>智能家居</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>znjj</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4023</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>13921.5244</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>中证白酒</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>zzbj</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4024</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2348.9773</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>中证煤炭</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2038-01-01 00:00:00+08:00</td>\n",
       "      <td></td>\n",
       "      <td>zzmt</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>NaT</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4025 rows × 32 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      adj_factor  multiplier  is_suspended  margin_ratio  position  \\\n",
       "0       121.3988         1.0             0           1.0         0   \n",
       "1       151.4445         1.0             0           1.0         0   \n",
       "2         7.4454         1.0             0           1.0         0   \n",
       "3        49.2924         1.0             0           1.0         0   \n",
       "4        26.8933         1.0             0           1.0         0   \n",
       "...          ...         ...           ...           ...       ...   \n",
       "4020      0.0000         1.0             0           1.0         0   \n",
       "4021      0.0000         1.0             0           1.0         0   \n",
       "4022      0.0000         1.0             0           1.0         0   \n",
       "4023      0.0000         1.0             0           1.0         0   \n",
       "4024      0.0000         1.0             0           1.0         0   \n",
       "\n",
       "      lower_limit   pre_close  settle_price  upper_limit  sec_level  ...  \\\n",
       "0           14.18     15.7500           0.0        17.33          1  ...   \n",
       "1           18.59     20.6600           0.0        22.73          1  ...   \n",
       "2           17.60     19.5600           0.0        21.52          1  ...   \n",
       "3            4.88      5.4200           0.0         5.96          1  ...   \n",
       "4            2.30      2.5500           0.0         2.81          1  ...   \n",
       "...           ...         ...           ...          ...        ...  ...   \n",
       "4020         0.00   1564.6021           0.0         0.00          1  ...   \n",
       "4021         0.00   4331.6421           0.0         0.00          1  ...   \n",
       "4022         0.00   2693.2241           0.0         0.00          1  ...   \n",
       "4023         0.00  13921.5244           0.0         0.00          1  ...   \n",
       "4024         0.00   2348.9773           0.0         0.00          1  ...   \n",
       "\n",
       "     board sec_name price_tick             delisted_date  call_or_put  \\\n",
       "0        1     平安银行       0.01 2038-01-01 00:00:00+08:00                \n",
       "1        1      万科A       0.01 2038-01-01 00:00:00+08:00                \n",
       "2        1     国华网安       0.01 2038-01-01 00:00:00+08:00                \n",
       "3        1     深振业A       0.01 2038-01-01 00:00:00+08:00                \n",
       "4        1     神州高铁       0.01 2038-01-01 00:00:00+08:00                \n",
       "...    ...      ...        ...                       ...          ...   \n",
       "4020     0     信息安全       0.00 2038-01-01 00:00:00+08:00                \n",
       "4021     0     基建工程       0.00 2038-01-01 00:00:00+08:00                \n",
       "4022     0     智能家居       0.00 2038-01-01 00:00:00+08:00                \n",
       "4023     0     中证白酒       0.00 2038-01-01 00:00:00+08:00                \n",
       "4024     0     中证煤炭       0.00 2038-01-01 00:00:00+08:00                \n",
       "\n",
       "     sec_abbr option_margin_ratio2 sec_type  conversion_start_date  \\\n",
       "0        payx                  0.0        1                    NaT   \n",
       "1         wkA                  0.0        1                    NaT   \n",
       "2        ghwa                  0.0        1                    NaT   \n",
       "3        szyA                  0.0        1                    NaT   \n",
       "4        szgt                  0.0        1                    NaT   \n",
       "...       ...                  ...      ...                    ...   \n",
       "4020     xxaq                  0.0        3                    NaT   \n",
       "4021     jjgc                  0.0        3                    NaT   \n",
       "4022     znjj                  0.0        3                    NaT   \n",
       "4023     zzbj                  0.0        3                    NaT   \n",
       "4024     zzmt                  0.0        3                    NaT   \n",
       "\n",
       "      is_adjusted  \n",
       "0               0  \n",
       "1               0  \n",
       "2               0  \n",
       "3               0  \n",
       "4               0  \n",
       "...           ...  \n",
       "4020            0  \n",
       "4021            0  \n",
       "4022            0  \n",
       "4023            0  \n",
       "4024            0  \n",
       "\n",
       "[4025 rows x 32 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交易所所有股票信息\n",
    "ex5=get_instruments(exchanges='SZSE', df=True)\n",
    "ex5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get_industry - 查询行业股票列表\n",
    "\n",
    "\n",
    "行业代码\n",
    "\n",
    "https://www.myquant.cn/docs/data/python_second_industry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['SHSE.600097',\n",
       " 'SHSE.600108',\n",
       " 'SHSE.600257',\n",
       " 'SHSE.600265',\n",
       " 'SHSE.600313',\n",
       " 'SHSE.600359',\n",
       " 'SHSE.600371',\n",
       " 'SHSE.600506',\n",
       " 'SHSE.600598',\n",
       " 'SHSE.600354',\n",
       " 'SHSE.600467',\n",
       " 'SHSE.600540',\n",
       " 'SHSE.600975',\n",
       " 'SZSE.000592',\n",
       " 'SZSE.000713',\n",
       " 'SZSE.000735',\n",
       " 'SZSE.000798',\n",
       " 'SZSE.000998',\n",
       " 'SZSE.002041',\n",
       " 'SZSE.002069',\n",
       " 'SZSE.002086',\n",
       " 'SZSE.002200',\n",
       " 'SZSE.002234',\n",
       " 'SZSE.200992',\n",
       " 'SZSE.002299',\n",
       " 'SZSE.002321',\n",
       " 'SZSE.300087',\n",
       " 'SZSE.300094',\n",
       " 'SZSE.002458',\n",
       " 'SZSE.300106',\n",
       " 'SHSE.601118',\n",
       " 'SZSE.300189',\n",
       " 'SZSE.300313',\n",
       " 'SZSE.002679',\n",
       " 'SZSE.002696',\n",
       " 'SZSE.002714',\n",
       " 'SZSE.002746',\n",
       " 'SZSE.002772',\n",
       " 'SZSE.300498',\n",
       " 'SZSE.300511',\n",
       " 'SHSE.603477',\n",
       " 'SZSE.140006',\n",
       " 'SZSE.300761',\n",
       " 'SZSE.002982',\n",
       " 'SZSE.300967',\n",
       " 'SZSE.300970',\n",
       " 'SZSE.300972',\n",
       " 'SZSE.001201',\n",
       " 'SZSE.300970',\n",
       " 'SZSE.300967',\n",
       " 'SZSE.300972',\n",
       " 'SHSE.605296',\n",
       " 'SZSE.001201']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#返回所有以J6开头的行业代码对应的成分股(包括:J66,J67,J68,J69的成分股)\n",
    "get_industry(code='A0')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get_constituents - 查询指数最新成份股\n",
    "\n",
    "get_constituents(index, fields=None, df=False)\n",
    "\n",
    "\n",
    "各种指数代码\n",
    "\n",
    "https://www.myquant.cn/docs/data/data_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.688001</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHSE.688002</td>\n",
       "      <td>0.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.688003</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SHSE.688005</td>\n",
       "      <td>0.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SHSE.688010</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1557</th>\n",
       "      <td>SHSE.900952</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1558</th>\n",
       "      <td>SHSE.900947</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1559</th>\n",
       "      <td>SHSE.900948</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1560</th>\n",
       "      <td>SHSE.900953</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1561</th>\n",
       "      <td>SHSE.900957</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1562 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           symbol  weight\n",
       "0     SHSE.688001    0.03\n",
       "1     SHSE.688002    0.10\n",
       "2     SHSE.688003    0.01\n",
       "3     SHSE.688005    0.07\n",
       "4     SHSE.688010    0.01\n",
       "...           ...     ...\n",
       "1557  SHSE.900952    0.00\n",
       "1558  SHSE.900947    0.01\n",
       "1559  SHSE.900948    0.01\n",
       "1560  SHSE.900953    0.00\n",
       "1561  SHSE.900957    0.00\n",
       "\n",
       "[1562 rows x 2 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_constituents(index='SHSE.000001', fields='symbol, weight', df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_instruments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## history_n - 查询历史行情最新n条\n",
    "\n",
    "history_n(symbol, frequency, count, end_time=None, fields=None, skip_suspended=True, \n",
    "          fill_missing=None, adjust=ADJUST_NONE, adjust_end_time='', df=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>high</th>\n",
       "      <th>eob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1653.7567</td>\n",
       "      <td>1636.8856</td>\n",
       "      <td>1636.8856</td>\n",
       "      <td>1653.8262</td>\n",
       "      <td>2020-09-29 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1644.5138</td>\n",
       "      <td>1652.9344</td>\n",
       "      <td>1644.5138</td>\n",
       "      <td>1676.1162</td>\n",
       "      <td>2020-09-30 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1676.2153</td>\n",
       "      <td>1680.1780</td>\n",
       "      <td>1672.2526</td>\n",
       "      <td>1690.5009</td>\n",
       "      <td>2020-10-09 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1684.1406</td>\n",
       "      <td>1734.2784</td>\n",
       "      <td>1684.1406</td>\n",
       "      <td>1734.2784</td>\n",
       "      <td>2020-10-12 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1734.2883</td>\n",
       "      <td>1722.7769</td>\n",
       "      <td>1718.8142</td>\n",
       "      <td>1734.2883</td>\n",
       "      <td>2020-10-13 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1723.7675</td>\n",
       "      <td>1712.8701</td>\n",
       "      <td>1709.8981</td>\n",
       "      <td>1730.6823</td>\n",
       "      <td>2020-10-14 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1705.0736</td>\n",
       "      <td>1708.9073</td>\n",
       "      <td>1704.9447</td>\n",
       "      <td>1718.8142</td>\n",
       "      <td>2020-10-15 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1708.9073</td>\n",
       "      <td>1696.0287</td>\n",
       "      <td>1687.1127</td>\n",
       "      <td>1718.3979</td>\n",
       "      <td>2020-10-16 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1708.9073</td>\n",
       "      <td>1683.1500</td>\n",
       "      <td>1676.1162</td>\n",
       "      <td>1717.8036</td>\n",
       "      <td>2020-10-19 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>SHSE.600519</td>\n",
       "      <td>1684.1307</td>\n",
       "      <td>1717.8234</td>\n",
       "      <td>1679.1874</td>\n",
       "      <td>1718.7844</td>\n",
       "      <td>2020-10-20 00:00:00+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        symbol       open      close        low       high  \\\n",
       "0  SHSE.600519  1653.7567  1636.8856  1636.8856  1653.8262   \n",
       "1  SHSE.600519  1644.5138  1652.9344  1644.5138  1676.1162   \n",
       "2  SHSE.600519  1676.2153  1680.1780  1672.2526  1690.5009   \n",
       "3  SHSE.600519  1684.1406  1734.2784  1684.1406  1734.2784   \n",
       "4  SHSE.600519  1734.2883  1722.7769  1718.8142  1734.2883   \n",
       "5  SHSE.600519  1723.7675  1712.8701  1709.8981  1730.6823   \n",
       "6  SHSE.600519  1705.0736  1708.9073  1704.9447  1718.8142   \n",
       "7  SHSE.600519  1708.9073  1696.0287  1687.1127  1718.3979   \n",
       "8  SHSE.600519  1708.9073  1683.1500  1676.1162  1717.8036   \n",
       "9  SHSE.600519  1684.1307  1717.8234  1679.1874  1718.7844   \n",
       "\n",
       "                        eob  \n",
       "0 2020-09-29 00:00:00+08:00  \n",
       "1 2020-09-30 00:00:00+08:00  \n",
       "2 2020-10-09 00:00:00+08:00  \n",
       "3 2020-10-12 00:00:00+08:00  \n",
       "4 2020-10-13 00:00:00+08:00  \n",
       "5 2020-10-14 00:00:00+08:00  \n",
       "6 2020-10-15 00:00:00+08:00  \n",
       "7 2020-10-16 00:00:00+08:00  \n",
       "8 2020-10-19 00:00:00+08:00  \n",
       "9 2020-10-20 00:00:00+08:00  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "history_n_data = history_n(symbol='SHSE.600519', frequency='1d', count=10, end_time='2020-10-20 15:30:00', fields='symbol, open, close, low, high, eob', adjust=ADJUST_PREV, df=True)\n",
    "history_n_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_sb=['SHSE.000910', 'SHSE.000909', 'SHSE.000911', 'SHSE.000912', 'SHSE.000913', 'SHSE.000914']\n",
    "# 待轮动的行业指数(分别为：300工业.300材料.300可选.300消费.300医药.300金融)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## classwork 1\n",
    "\n",
    "1 获取“酒、饮料和精制茶制造业”行业前十个股票的信息\n",
    "\n",
    "2 获取“380信息” 前十个股票信息\n",
    "\n",
    "3 查询“酒、饮料和精制茶制造业”行业前十个股票的上个月的收盘价\n",
    "\n",
    "4，实现策略：2021年初购买“380信息” 前十个股票，看现在的收益情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>return</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [return]\n",
       "Index: []"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(columns=['return'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SHSE.000912\n"
     ]
    }
   ],
   "source": [
    "r1=0\n",
    "for i in index_sb:\n",
    "    index_data = history_n(symbol=i, frequency='1d', count=21, end_time='2021-10-20 15:30:00', fields='close, eob', adjust=ADJUST_PREV, df=True)\n",
    "    r=index_data.close.iloc[-1]/index_data.close.iloc[0]\n",
    "    if r>r1:\n",
    "        r1=r\n",
    "        sb=i\n",
    "print(sb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['SHSE.600059',\n",
       " 'SHSE.600084',\n",
       " 'SHSE.600132',\n",
       " 'SHSE.600189',\n",
       " 'SHSE.600197',\n",
       " 'SHSE.600199',\n",
       " 'SHSE.600238',\n",
       " 'SHSE.600300',\n",
       " 'SHSE.600365',\n",
       " 'SHSE.600519']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_industry(code='C15')[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['SHSE.688008',\n",
       " 'SHSE.688099',\n",
       " 'SHSE.600563',\n",
       " 'SHSE.603613',\n",
       " 'SHSE.605358',\n",
       " 'SHSE.688036',\n",
       " 'SHSE.603290',\n",
       " 'SHSE.603267',\n",
       " 'SHSE.600845',\n",
       " 'SHSE.603893']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_constituents(index='SHSE.000111', fields='symbol, weight', df=True).sort_values(by=\"weight\",ascending=False).symbol.tolist()[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      13.06\n",
       "1       3.51\n",
       "2     171.55\n",
       "3      12.72\n",
       "4      30.32\n",
       "5      15.30\n",
       "6      15.21\n",
       "7       3.58\n",
       "8       2.99\n",
       "9    2071.29\n",
       "Name: pre_close, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_instruments(symbols=get_industry(code='C15')[0:10], skip_st=False,df=True).pre_close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      13.06\n",
       "1     171.55\n",
       "2      12.72\n",
       "3      30.32\n",
       "4      15.30\n",
       "5      15.21\n",
       "6    2071.29\n",
       "Name: pre_close, dtype: float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_instruments(symbols=get_industry(code='C15')[0:10], df=True).pre_close"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 财务数据\n",
    "\n",
    "https://www.myquant.cn/docs/data/data_derive\n",
    "\n",
    "股票交易衍生表 表名: trading_derivative_indicator（有股价参与的量随时间变化快，没有股价参与则变化慢）\n",
    "\n",
    "资产负债表  表名: balance_sheet\n",
    "\n",
    "现金流量表  表名: cashflow_statement\n",
    "\n",
    "利润表  表名: income_statement\n",
    "\n",
    "主要财务指标  表名: prim_finance_indicator\n",
    "\n",
    "衍生财务指标 表名: deriv_finance_indicator\n",
    "\n",
    "## 重要的财务指标\n",
    "\n",
    "视频：\n",
    "\n",
    "https://www.zhihu.com/zvideo/1378719325843496960\n",
    "\n",
    "https://www.bilibili.com/video/BV1of4y1W7uA?from=search&seid=14568563005615111318\n",
    "\n",
    "https://www.bilibili.com/video/BV1Sz4y197vh?from=search&seid=14568563005615111318\n",
    "\n",
    "视频课程：\n",
    "\n",
    "https://www.bilibili.com/video/BV1UV411677D?from=search&seid=1487702117528889861"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取财务数据函数\n",
    "\n",
    "\n",
    "## get_fundamentals - 查询基本面数据\n",
    "\n",
    "get_fundamentals(table, symbols, start_date, end_date, fields=None, filter=None, order_by=None, limit=1000, df=False)\n",
    "\n",
    "\n",
    "1.当 start_date == end_date 时, 取所举每个股票离 end_date 最近业务日期(交易日期或报告日期) 一条数据,当 start_date < end_date 时, 取指定时间段的数据,当 start_date > end_date 时, 返回空list/空DataFrame\n",
    "\n",
    "2.当不指定排序方式时,返回的list/DataFrame以参数pub_date/end_date来排序\n",
    "\n",
    "3.start_date和end_date中月,日均可以只输入个位数,例:'2010-7-8'或'2017-7-30'\n",
    "\n",
    "4.若输入包含无效标的代码，则返回的list/DataFrame只包含有效标的代码对应的数据\n",
    "\n",
    "5.在该函数中，table参数只支持输入一个表名，若表名输入错误或以'table1,table2'方式输入多个表名，函数返回空list/空DataFrame\n",
    "\n",
    "6.若表名输入正确，但查询字段中出现非指定字符串，则程序直接报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>pub_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>UNDIPROF</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2017-04-27 00:00:00+08:00</td>\n",
       "      <td>2017-03-31 00:00:00+08:00</td>\n",
       "      <td>1.1085e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2017-04-22 00:00:00+08:00</td>\n",
       "      <td>2017-03-31 00:00:00+08:00</td>\n",
       "      <td>6.9483e+10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        symbol                  pub_date                  end_date    UNDIPROF\n",
       "0  SHSE.600000 2017-04-27 00:00:00+08:00 2017-03-31 00:00:00+08:00  1.1085e+11\n",
       "1  SZSE.000001 2017-04-22 00:00:00+08:00 2017-03-31 00:00:00+08:00  6.9483e+10"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex1=get_fundamentals(table='balance_sheet', symbols=['SHSE.600000', 'SZSE.000001'],\n",
    "                     start_date='2017-01-01', end_date='2017-06-01',\n",
    "                     fields='UNDIPROF', df=True)\n",
    "ex1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>pub_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>TCLOSE</th>\n",
       "      <th>NEGOTIABLEMV</th>\n",
       "      <th>TOTMKTCAP</th>\n",
       "      <th>TURNRATE</th>\n",
       "      <th>PELFY</th>\n",
       "      <th>PETTM</th>\n",
       "      <th>PEMRQ</th>\n",
       "      <th>PELFYNPAAEI</th>\n",
       "      <th>PETTMNPAAEI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-01-04 00:00:00+08:00</td>\n",
       "      <td>2016-01-04 00:00:00+08:00</td>\n",
       "      <td>17.80</td>\n",
       "      <td>3.3203e+11</td>\n",
       "      <td>3.3203e+11</td>\n",
       "      <td>0.2264</td>\n",
       "      <td>7.0606</td>\n",
       "      <td>6.7228</td>\n",
       "      <td>6.7010</td>\n",
       "      <td>7.1175</td>\n",
       "      <td>6.8409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-01-04 00:00:00+08:00</td>\n",
       "      <td>2016-01-04 00:00:00+08:00</td>\n",
       "      <td>11.33</td>\n",
       "      <td>1.3374e+11</td>\n",
       "      <td>1.6212e+11</td>\n",
       "      <td>0.4774</td>\n",
       "      <td>8.1869</td>\n",
       "      <td>7.4202</td>\n",
       "      <td>6.8539</td>\n",
       "      <td>8.1708</td>\n",
       "      <td>6.8485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-01-05 00:00:00+08:00</td>\n",
       "      <td>2016-01-05 00:00:00+08:00</td>\n",
       "      <td>17.96</td>\n",
       "      <td>3.3502e+11</td>\n",
       "      <td>3.3502e+11</td>\n",
       "      <td>0.3112</td>\n",
       "      <td>7.1241</td>\n",
       "      <td>6.7832</td>\n",
       "      <td>6.7613</td>\n",
       "      <td>7.1815</td>\n",
       "      <td>6.9024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-01-05 00:00:00+08:00</td>\n",
       "      <td>2016-01-05 00:00:00+08:00</td>\n",
       "      <td>11.40</td>\n",
       "      <td>1.3457e+11</td>\n",
       "      <td>1.6312e+11</td>\n",
       "      <td>0.5619</td>\n",
       "      <td>8.2375</td>\n",
       "      <td>7.4661</td>\n",
       "      <td>6.8962</td>\n",
       "      <td>8.2213</td>\n",
       "      <td>6.8908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-01-06 00:00:00+08:00</td>\n",
       "      <td>2016-01-06 00:00:00+08:00</td>\n",
       "      <td>18.10</td>\n",
       "      <td>3.3763e+11</td>\n",
       "      <td>3.3763e+11</td>\n",
       "      <td>0.2507</td>\n",
       "      <td>7.1796</td>\n",
       "      <td>6.8361</td>\n",
       "      <td>6.8140</td>\n",
       "      <td>7.2375</td>\n",
       "      <td>6.9562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>483</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-12-28 00:00:00+08:00</td>\n",
       "      <td>2016-12-28 00:00:00+08:00</td>\n",
       "      <td>9.06</td>\n",
       "      <td>1.3256e+11</td>\n",
       "      <td>1.5556e+11</td>\n",
       "      <td>0.2297</td>\n",
       "      <td>7.1147</td>\n",
       "      <td>6.8098</td>\n",
       "      <td>6.2329</td>\n",
       "      <td>7.1027</td>\n",
       "      <td>6.2369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>484</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-12-29 00:00:00+08:00</td>\n",
       "      <td>2016-12-29 00:00:00+08:00</td>\n",
       "      <td>16.07</td>\n",
       "      <td>3.2974e+11</td>\n",
       "      <td>3.4741e+11</td>\n",
       "      <td>0.0578</td>\n",
       "      <td>6.8652</td>\n",
       "      <td>6.4187</td>\n",
       "      <td>6.4047</td>\n",
       "      <td>7.0097</td>\n",
       "      <td>6.5613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>485</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-12-29 00:00:00+08:00</td>\n",
       "      <td>2016-12-29 00:00:00+08:00</td>\n",
       "      <td>9.08</td>\n",
       "      <td>1.3285e+11</td>\n",
       "      <td>1.5591e+11</td>\n",
       "      <td>0.2315</td>\n",
       "      <td>7.1305</td>\n",
       "      <td>6.8249</td>\n",
       "      <td>6.2466</td>\n",
       "      <td>7.1184</td>\n",
       "      <td>6.2506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>486</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-12-30 00:00:00+08:00</td>\n",
       "      <td>2016-12-30 00:00:00+08:00</td>\n",
       "      <td>16.21</td>\n",
       "      <td>3.3261e+11</td>\n",
       "      <td>3.5043e+11</td>\n",
       "      <td>0.0598</td>\n",
       "      <td>6.9250</td>\n",
       "      <td>6.4746</td>\n",
       "      <td>6.4605</td>\n",
       "      <td>7.0707</td>\n",
       "      <td>6.6184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>487</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-12-30 00:00:00+08:00</td>\n",
       "      <td>2016-12-30 00:00:00+08:00</td>\n",
       "      <td>9.10</td>\n",
       "      <td>1.3314e+11</td>\n",
       "      <td>1.5625e+11</td>\n",
       "      <td>0.2068</td>\n",
       "      <td>7.1462</td>\n",
       "      <td>6.8399</td>\n",
       "      <td>6.2604</td>\n",
       "      <td>7.1341</td>\n",
       "      <td>6.2644</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>488 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          symbol                  pub_date                  end_date  TCLOSE  \\\n",
       "0    SHSE.600000 2016-01-04 00:00:00+08:00 2016-01-04 00:00:00+08:00   17.80   \n",
       "1    SZSE.000001 2016-01-04 00:00:00+08:00 2016-01-04 00:00:00+08:00   11.33   \n",
       "2    SHSE.600000 2016-01-05 00:00:00+08:00 2016-01-05 00:00:00+08:00   17.96   \n",
       "3    SZSE.000001 2016-01-05 00:00:00+08:00 2016-01-05 00:00:00+08:00   11.40   \n",
       "4    SHSE.600000 2016-01-06 00:00:00+08:00 2016-01-06 00:00:00+08:00   18.10   \n",
       "..           ...                       ...                       ...     ...   \n",
       "483  SZSE.000001 2016-12-28 00:00:00+08:00 2016-12-28 00:00:00+08:00    9.06   \n",
       "484  SHSE.600000 2016-12-29 00:00:00+08:00 2016-12-29 00:00:00+08:00   16.07   \n",
       "485  SZSE.000001 2016-12-29 00:00:00+08:00 2016-12-29 00:00:00+08:00    9.08   \n",
       "486  SHSE.600000 2016-12-30 00:00:00+08:00 2016-12-30 00:00:00+08:00   16.21   \n",
       "487  SZSE.000001 2016-12-30 00:00:00+08:00 2016-12-30 00:00:00+08:00    9.10   \n",
       "\n",
       "     NEGOTIABLEMV   TOTMKTCAP  TURNRATE   PELFY   PETTM   PEMRQ  PELFYNPAAEI  \\\n",
       "0      3.3203e+11  3.3203e+11    0.2264  7.0606  6.7228  6.7010       7.1175   \n",
       "1      1.3374e+11  1.6212e+11    0.4774  8.1869  7.4202  6.8539       8.1708   \n",
       "2      3.3502e+11  3.3502e+11    0.3112  7.1241  6.7832  6.7613       7.1815   \n",
       "3      1.3457e+11  1.6312e+11    0.5619  8.2375  7.4661  6.8962       8.2213   \n",
       "4      3.3763e+11  3.3763e+11    0.2507  7.1796  6.8361  6.8140       7.2375   \n",
       "..            ...         ...       ...     ...     ...     ...          ...   \n",
       "483    1.3256e+11  1.5556e+11    0.2297  7.1147  6.8098  6.2329       7.1027   \n",
       "484    3.2974e+11  3.4741e+11    0.0578  6.8652  6.4187  6.4047       7.0097   \n",
       "485    1.3285e+11  1.5591e+11    0.2315  7.1305  6.8249  6.2466       7.1184   \n",
       "486    3.3261e+11  3.5043e+11    0.0598  6.9250  6.4746  6.4605       7.0707   \n",
       "487    1.3314e+11  1.5625e+11    0.2068  7.1462  6.8399  6.2604       7.1341   \n",
       "\n",
       "     PETTMNPAAEI  \n",
       "0         6.8409  \n",
       "1         6.8485  \n",
       "2         6.9024  \n",
       "3         6.8908  \n",
       "4         6.9562  \n",
       "..           ...  \n",
       "483       6.2369  \n",
       "484       6.5613  \n",
       "485       6.2506  \n",
       "486       6.6184  \n",
       "487       6.2644  \n",
       "\n",
       "[488 rows x 12 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex2=get_fundamentals(table='trading_derivative_indicator', symbols='SHSE.600000, SZSE.000001', start_date='2016-01-01', end_date='2017-01-01',\n",
    "                 fields='TCLOSE,NEGOTIABLEMV,TOTMKTCAP,TURNRATE,PELFY,PETTM,PEMRQ,PELFYNPAAEI,PETTMNPAAEI', df=True)\n",
    "ex2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>pub_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>TCLOSE</th>\n",
       "      <th>NEGOTIABLEMV</th>\n",
       "      <th>TOTMKTCAP</th>\n",
       "      <th>TURNRATE</th>\n",
       "      <th>PELFY</th>\n",
       "      <th>PETTM</th>\n",
       "      <th>PEMRQ</th>\n",
       "      <th>PELFYNPAAEI</th>\n",
       "      <th>PETTMNPAAEI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.600002</td>\n",
       "      <td>2006-04-24 00:00:00+08:00</td>\n",
       "      <td>2006-04-24 00:00:00+08:00</td>\n",
       "      <td>10.09</td>\n",
       "      <td>3.5290e+09</td>\n",
       "      <td>1.9676e+10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.7040</td>\n",
       "      <td>10.7040</td>\n",
       "      <td>10.7040</td>\n",
       "      <td>10.6744</td>\n",
       "      <td>10.6744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHSE.600001</td>\n",
       "      <td>2009-12-29 00:00:00+08:00</td>\n",
       "      <td>2009-12-29 00:00:00+08:00</td>\n",
       "      <td>5.29</td>\n",
       "      <td>1.4899e+10</td>\n",
       "      <td>1.4899e+10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>24.8622</td>\n",
       "      <td>-673.6525</td>\n",
       "      <td>55.6226</td>\n",
       "      <td>22.6761</td>\n",
       "      <td>55.6510</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        symbol                  pub_date                  end_date  TCLOSE  \\\n",
       "0  SHSE.600002 2006-04-24 00:00:00+08:00 2006-04-24 00:00:00+08:00   10.09   \n",
       "1  SHSE.600001 2009-12-29 00:00:00+08:00 2009-12-29 00:00:00+08:00    5.29   \n",
       "\n",
       "   NEGOTIABLEMV   TOTMKTCAP  TURNRATE    PELFY     PETTM    PEMRQ  \\\n",
       "0    3.5290e+09  1.9676e+10       0.0  10.7040   10.7040  10.7040   \n",
       "1    1.4899e+10  1.4899e+10       0.0  24.8622 -673.6525  55.6226   \n",
       "\n",
       "   PELFYNPAAEI  PETTMNPAAEI  \n",
       "0      10.6744      10.6744  \n",
       "1      22.6761      55.6510  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_symbols = ['SHSE.600000', 'SHSE.600001', 'SHSE.600002']#注意格式\n",
    "ex3=get_fundamentals(table='trading_derivative_indicator', start_date='2017-01-01', end_date='2017-01-01', filter='PCTTM > 0 and PCTTM < 10', symbols=my_symbols,\n",
    "                     fields='TCLOSE,NEGOTIABLEMV,TOTMKTCAP,TURNRATE,PELFY,PETTM,PEMRQ,PELFYNPAAEI,PETTMNPAAEI', df=True)\n",
    "ex3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get_fundamentals_n - 查询基本面数据最新n条\n",
    "\n",
    "取指定股票的最近 end_date 的 count 条记录\n",
    "\n",
    "get_fundamentals_n(table, symbols, end_date, fields=None, filter=None, order_by=None, count=1, df=False)\n",
    "\n",
    "\n",
    "\n",
    "1.对每个标的,返回的list/DataFrame以参数pub_date/end_date的倒序来排序\n",
    "\n",
    "2.end_date中月,日均可以只输入个位数,例:'2010-7-8'或'2017-7-30'\n",
    "\n",
    "3.若输入包含无效标的代码，则返回的list/DataFrame只包含有效标的代码对应的数据\n",
    "\n",
    "4.在该函数中，table参数只支持输入一个表名，若表名输入错误或以'table1,table2'方式输入多个表名，函数返回空list/空DataFrame\n",
    "\n",
    "5.若表名输入正确，但查询字段中出现非指定字符串，则程序直接报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>pub_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>TCLOSE</th>\n",
       "      <th>NEGOTIABLEMV</th>\n",
       "      <th>TOTMKTCAP</th>\n",
       "      <th>TURNRATE</th>\n",
       "      <th>PELFY</th>\n",
       "      <th>PETTM</th>\n",
       "      <th>PEMRQ</th>\n",
       "      <th>PELFYNPAAEI</th>\n",
       "      <th>PETTMNPAAEI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-12-30 00:00:00+08:00</td>\n",
       "      <td>2016-12-30 00:00:00+08:00</td>\n",
       "      <td>16.21</td>\n",
       "      <td>3.3261e+11</td>\n",
       "      <td>3.5043e+11</td>\n",
       "      <td>0.0598</td>\n",
       "      <td>6.9250</td>\n",
       "      <td>6.4746</td>\n",
       "      <td>6.4605</td>\n",
       "      <td>7.0707</td>\n",
       "      <td>6.6184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-12-29 00:00:00+08:00</td>\n",
       "      <td>2016-12-29 00:00:00+08:00</td>\n",
       "      <td>16.07</td>\n",
       "      <td>3.2974e+11</td>\n",
       "      <td>3.4741e+11</td>\n",
       "      <td>0.0578</td>\n",
       "      <td>6.8652</td>\n",
       "      <td>6.4187</td>\n",
       "      <td>6.4047</td>\n",
       "      <td>7.0097</td>\n",
       "      <td>6.5613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-12-28 00:00:00+08:00</td>\n",
       "      <td>2016-12-28 00:00:00+08:00</td>\n",
       "      <td>16.09</td>\n",
       "      <td>3.3015e+11</td>\n",
       "      <td>3.4784e+11</td>\n",
       "      <td>0.0704</td>\n",
       "      <td>6.8737</td>\n",
       "      <td>6.4267</td>\n",
       "      <td>6.4126</td>\n",
       "      <td>7.0184</td>\n",
       "      <td>6.5694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-12-30 00:00:00+08:00</td>\n",
       "      <td>2016-12-30 00:00:00+08:00</td>\n",
       "      <td>9.10</td>\n",
       "      <td>1.3314e+11</td>\n",
       "      <td>1.5625e+11</td>\n",
       "      <td>0.2068</td>\n",
       "      <td>7.1462</td>\n",
       "      <td>6.8399</td>\n",
       "      <td>6.2604</td>\n",
       "      <td>7.1341</td>\n",
       "      <td>6.2644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-12-29 00:00:00+08:00</td>\n",
       "      <td>2016-12-29 00:00:00+08:00</td>\n",
       "      <td>9.08</td>\n",
       "      <td>1.3285e+11</td>\n",
       "      <td>1.5591e+11</td>\n",
       "      <td>0.2315</td>\n",
       "      <td>7.1305</td>\n",
       "      <td>6.8249</td>\n",
       "      <td>6.2466</td>\n",
       "      <td>7.1184</td>\n",
       "      <td>6.2506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-12-28 00:00:00+08:00</td>\n",
       "      <td>2016-12-28 00:00:00+08:00</td>\n",
       "      <td>9.06</td>\n",
       "      <td>1.3256e+11</td>\n",
       "      <td>1.5556e+11</td>\n",
       "      <td>0.2297</td>\n",
       "      <td>7.1147</td>\n",
       "      <td>6.8098</td>\n",
       "      <td>6.2329</td>\n",
       "      <td>7.1027</td>\n",
       "      <td>6.2369</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        symbol                  pub_date                  end_date  TCLOSE  \\\n",
       "0  SHSE.600000 2016-12-30 00:00:00+08:00 2016-12-30 00:00:00+08:00   16.21   \n",
       "1  SHSE.600000 2016-12-29 00:00:00+08:00 2016-12-29 00:00:00+08:00   16.07   \n",
       "2  SHSE.600000 2016-12-28 00:00:00+08:00 2016-12-28 00:00:00+08:00   16.09   \n",
       "3  SZSE.000001 2016-12-30 00:00:00+08:00 2016-12-30 00:00:00+08:00    9.10   \n",
       "4  SZSE.000001 2016-12-29 00:00:00+08:00 2016-12-29 00:00:00+08:00    9.08   \n",
       "5  SZSE.000001 2016-12-28 00:00:00+08:00 2016-12-28 00:00:00+08:00    9.06   \n",
       "\n",
       "   NEGOTIABLEMV   TOTMKTCAP  TURNRATE   PELFY   PETTM   PEMRQ  PELFYNPAAEI  \\\n",
       "0    3.3261e+11  3.5043e+11    0.0598  6.9250  6.4746  6.4605       7.0707   \n",
       "1    3.2974e+11  3.4741e+11    0.0578  6.8652  6.4187  6.4047       7.0097   \n",
       "2    3.3015e+11  3.4784e+11    0.0704  6.8737  6.4267  6.4126       7.0184   \n",
       "3    1.3314e+11  1.5625e+11    0.2068  7.1462  6.8399  6.2604       7.1341   \n",
       "4    1.3285e+11  1.5591e+11    0.2315  7.1305  6.8249  6.2466       7.1184   \n",
       "5    1.3256e+11  1.5556e+11    0.2297  7.1147  6.8098  6.2329       7.1027   \n",
       "\n",
       "   PETTMNPAAEI  \n",
       "0       6.6184  \n",
       "1       6.5613  \n",
       "2       6.5694  \n",
       "3       6.2644  \n",
       "4       6.2506  \n",
       "5       6.2369  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex4=get_fundamentals_n(table='trading_derivative_indicator', symbols='SHSE.600000, SZSE.000001', end_date='2017-01-01', count=3,\n",
    "                   fields='TCLOSE,NEGOTIABLEMV,TOTMKTCAP,TURNRATE,PELFY,PETTM,PEMRQ,PELFYNPAAEI,PETTMNPAAEI',df=True)\n",
    "ex4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>pub_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>UNDIPROF</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2017-04-27 00:00:00+08:00</td>\n",
       "      <td>2017-03-31 00:00:00+08:00</td>\n",
       "      <td>1.1085e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2017-04-01 00:00:00+08:00</td>\n",
       "      <td>2016-12-31 00:00:00+08:00</td>\n",
       "      <td>9.7316e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.600000</td>\n",
       "      <td>2016-10-29 00:00:00+08:00</td>\n",
       "      <td>2016-09-30 00:00:00+08:00</td>\n",
       "      <td>8.5792e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2017-04-22 00:00:00+08:00</td>\n",
       "      <td>2017-03-31 00:00:00+08:00</td>\n",
       "      <td>6.9483e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2017-03-17 00:00:00+08:00</td>\n",
       "      <td>2016-12-31 00:00:00+08:00</td>\n",
       "      <td>6.4143e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SZSE.000001</td>\n",
       "      <td>2016-10-21 00:00:00+08:00</td>\n",
       "      <td>2016-09-30 00:00:00+08:00</td>\n",
       "      <td>6.9463e+10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        symbol                  pub_date                  end_date    UNDIPROF\n",
       "0  SHSE.600000 2017-04-27 00:00:00+08:00 2017-03-31 00:00:00+08:00  1.1085e+11\n",
       "1  SHSE.600000 2017-04-01 00:00:00+08:00 2016-12-31 00:00:00+08:00  9.7316e+10\n",
       "2  SHSE.600000 2016-10-29 00:00:00+08:00 2016-09-30 00:00:00+08:00  8.5792e+10\n",
       "3  SZSE.000001 2017-04-22 00:00:00+08:00 2017-03-31 00:00:00+08:00  6.9483e+10\n",
       "4  SZSE.000001 2017-03-17 00:00:00+08:00 2016-12-31 00:00:00+08:00  6.4143e+10\n",
       "5  SZSE.000001 2016-10-21 00:00:00+08:00 2016-09-30 00:00:00+08:00  6.9463e+10"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex1=get_fundamentals_n(table='balance_sheet', symbols=['SHSE.600000', 'SZSE.000001'],count=3,end_date='2017-06-01',\n",
    "                     fields='UNDIPROF', df=True)\n",
    "ex1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## classwork2\n",
    "\n",
    "1  获取“380信息”所有股票在2021年第一天的最近一期的市值\n",
    "\n",
    "2，获取上面市值排名前十的股票ID\n",
    "\n",
    "3，编写策略：在2021年1月1日购买“380信息”市值排名前十的股票，看现在收益"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "sy=get_constituents(index='SHSE.000111', fields='symbol, weight', df=True).sort_values(by=\"weight\",ascending=False).symbol.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>symbol</th>\n",
       "      <th>pub_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>NEGOTIABLEMV</th>\n",
       "      <th>TOTMKTCAP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHSE.603533</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.3474e+10</td>\n",
       "      <td>1.3474e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHSE.603508</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>5.4725e+09</td>\n",
       "      <td>6.6250e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHSE.603258</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>7.5908e+09</td>\n",
       "      <td>7.8043e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SHSE.603888</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>9.8616e+09</td>\n",
       "      <td>9.8616e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SHSE.603228</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2.5307e+10</td>\n",
       "      <td>2.5733e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SHSE.603444</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>3.0614e+10</td>\n",
       "      <td>3.0614e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>SHSE.603039</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2.1153e+10</td>\n",
       "      <td>2.1470e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>SHSE.603881</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.2658e+10</td>\n",
       "      <td>1.4123e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>SHSE.603626</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>5.5490e+09</td>\n",
       "      <td>5.6501e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>SHSE.603920</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.0649e+10</td>\n",
       "      <td>1.0712e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>SHSE.603989</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.0617e+10</td>\n",
       "      <td>1.0617e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>SHSE.603220</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2.6863e+09</td>\n",
       "      <td>4.8874e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>SHSE.603068</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>8.0294e+09</td>\n",
       "      <td>1.1829e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>SHSE.603267</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2.0281e+10</td>\n",
       "      <td>2.9805e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>SHSE.603327</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.9972e+09</td>\n",
       "      <td>7.8155e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>SHSE.688002</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2.5510e+10</td>\n",
       "      <td>4.9395e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>SHSE.688008</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>3.4826e+10</td>\n",
       "      <td>9.3639e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>SHSE.603613</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.5396e+10</td>\n",
       "      <td>3.0377e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>SHSE.603927</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.3013e+10</td>\n",
       "      <td>1.6998e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>SHSE.600667</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.9903e+10</td>\n",
       "      <td>1.9903e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>SHSE.600171</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>9.8085e+09</td>\n",
       "      <td>9.8718e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>SHSE.600206</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.1242e+10</td>\n",
       "      <td>1.1311e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>SHSE.600363</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.0923e+10</td>\n",
       "      <td>1.1218e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>SHSE.600446</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.3865e+10</td>\n",
       "      <td>1.3948e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>SHSE.600460</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>3.2802e+10</td>\n",
       "      <td>3.2802e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>SHSE.600556</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>4.4117e+09</td>\n",
       "      <td>2.2868e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>SHSE.600562</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.0592e+10</td>\n",
       "      <td>2.1136e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>SHSE.600563</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2.4199e+10</td>\n",
       "      <td>2.4199e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>SHSE.600131</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>7.6873e+09</td>\n",
       "      <td>1.7010e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>SHSE.600728</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.3129e+10</td>\n",
       "      <td>1.3339e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>SHSE.600756</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>5.0527e+09</td>\n",
       "      <td>5.0527e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>SHSE.600845</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>5.7693e+10</td>\n",
       "      <td>5.9185e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>SHSE.600850</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.1495e+10</td>\n",
       "      <td>1.1495e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>SHSE.601231</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>4.2224e+10</td>\n",
       "      <td>4.2726e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>SHSE.603000</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>1.8686e+10</td>\n",
       "      <td>1.8686e+10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>SHSE.603328</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>7.9376e+09</td>\n",
       "      <td>7.9376e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>SHSE.603678</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>2020-12-31 00:00:00+08:00</td>\n",
       "      <td>3.2728e+10</td>\n",
       "      <td>3.2728e+10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         symbol                  pub_date                  end_date  \\\n",
       "0   SHSE.603533 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "1   SHSE.603508 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "2   SHSE.603258 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "3   SHSE.603888 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "4   SHSE.603228 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "5   SHSE.603444 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "6   SHSE.603039 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "7   SHSE.603881 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "8   SHSE.603626 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "9   SHSE.603920 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "10  SHSE.603989 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "11  SHSE.603220 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "12  SHSE.603068 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "13  SHSE.603267 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "14  SHSE.603327 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "15  SHSE.688002 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "16  SHSE.688008 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "17  SHSE.603613 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "18  SHSE.603927 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "19  SHSE.600667 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "20  SHSE.600171 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "21  SHSE.600206 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "22  SHSE.600363 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "23  SHSE.600446 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "24  SHSE.600460 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "25  SHSE.600556 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "26  SHSE.600562 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "27  SHSE.600563 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "28  SHSE.600131 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "29  SHSE.600728 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "30  SHSE.600756 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "31  SHSE.600845 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "32  SHSE.600850 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "33  SHSE.601231 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "34  SHSE.603000 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "35  SHSE.603328 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "36  SHSE.603678 2020-12-31 00:00:00+08:00 2020-12-31 00:00:00+08:00   \n",
       "\n",
       "    NEGOTIABLEMV   TOTMKTCAP  \n",
       "0     1.3474e+10  1.3474e+10  \n",
       "1     5.4725e+09  6.6250e+09  \n",
       "2     7.5908e+09  7.8043e+09  \n",
       "3     9.8616e+09  9.8616e+09  \n",
       "4     2.5307e+10  2.5733e+10  \n",
       "5     3.0614e+10  3.0614e+10  \n",
       "6     2.1153e+10  2.1470e+10  \n",
       "7     1.2658e+10  1.4123e+10  \n",
       "8     5.5490e+09  5.6501e+09  \n",
       "9     1.0649e+10  1.0712e+10  \n",
       "10    1.0617e+10  1.0617e+10  \n",
       "11    2.6863e+09  4.8874e+09  \n",
       "12    8.0294e+09  1.1829e+10  \n",
       "13    2.0281e+10  2.9805e+10  \n",
       "14    1.9972e+09  7.8155e+09  \n",
       "15    2.5510e+10  4.9395e+10  \n",
       "16    3.4826e+10  9.3639e+10  \n",
       "17    1.5396e+10  3.0377e+10  \n",
       "18    1.3013e+10  1.6998e+10  \n",
       "19    1.9903e+10  1.9903e+10  \n",
       "20    9.8085e+09  9.8718e+09  \n",
       "21    1.1242e+10  1.1311e+10  \n",
       "22    1.0923e+10  1.1218e+10  \n",
       "23    1.3865e+10  1.3948e+10  \n",
       "24    3.2802e+10  3.2802e+10  \n",
       "25    4.4117e+09  2.2868e+10  \n",
       "26    1.0592e+10  2.1136e+10  \n",
       "27    2.4199e+10  2.4199e+10  \n",
       "28    7.6873e+09  1.7010e+10  \n",
       "29    1.3129e+10  1.3339e+10  \n",
       "30    5.0527e+09  5.0527e+09  \n",
       "31    5.7693e+10  5.9185e+10  \n",
       "32    1.1495e+10  1.1495e+10  \n",
       "33    4.2224e+10  4.2726e+10  \n",
       "34    1.8686e+10  1.8686e+10  \n",
       "35    7.9376e+09  7.9376e+09  \n",
       "36    3.2728e+10  3.2728e+10  "
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex1=get_fundamentals(table='trading_derivative_indicator', symbols=sy,\n",
    "                     start_date='2021-01-01', end_date='2021-01-01',\n",
    "                     fields='NEGOTIABLEMV,TOTMKTCAP', df=True)\n",
    "ex1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['SHSE.603220',\n",
       " 'SHSE.600756',\n",
       " 'SHSE.603626',\n",
       " 'SHSE.603508',\n",
       " 'SHSE.603258',\n",
       " 'SHSE.603327',\n",
       " 'SHSE.603328',\n",
       " 'SHSE.603888',\n",
       " 'SHSE.600171',\n",
       " 'SHSE.603989']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex1.sort_values(by=\"TOTMKTCAP\").head(10).symbol.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
